package org.flex.chat;

import java.io.IOException;
import java.util.Vector;

public class Room {
	private String code, name;
	private Vector<RequestProccess> v = new Vector<RequestProccess>(),
			bad = new Vector<RequestProccess>();

	public Room(String code, String name) {
		super();
		this.code = code;
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Vector<RequestProccess> getV() {
		return v;
	}

	public void add(RequestProccess rp) {
		synchronized (v) {
			v.addElement(rp);
		}
	}

	public void setV(Vector<RequestProccess> v) {
		this.v = v;
	}

	public Vector<RequestProccess> boastMessage(String mes) {
		bad.clear();
		synchronized (v) {
			for (RequestProccess rp : v) {
				try {
					rp.sendMessage(mes);
				} catch (IOException e) {
					bad.add(rp);
				}
			}
		}
		if (!bad.isEmpty()) {
			synchronized (v) {
				for (RequestProccess rp : bad)
					v.remove(rp);
			}
			return bad;
		}
		return null;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Room other = (Room) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
